package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        if (checkgroupIds !=null && checkgroupIds.length > 0){
            setSetmealAndCheckGroupId(setmeal, checkgroupIds);
        }
        //图片保存成功后,将图片名称保存到Redis
        jedisPool(setmeal);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        return setmealDao.findCheckGroupIdsBySetmealId(id);
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //根据套餐的id删除中间表数据（清理原有关联关系）
        setmealDao.deleteAssociation(setmeal.getId());
        //向中间表(t_setmeal_checkgroup)插入数据（建立检查组和检查项关联关系）
        setSetmealAndCheckGroupId(setmeal,checkgroupIds);
        //更新套餐的数据
        setmealDao.edit(setmeal);
        //修改成功后，将名称保存到Redis
        jedisPool(setmeal);
    }

    /**
     * 提取方法，数据修改和保存的时候都要将图片保存到jedis数据库中
     * @param setmeal
     */
    public void jedisPool(Setmeal setmeal) {
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
    }

    /**
     * 提取方法，绑定套餐和检查组的多对多关系
     * @param setmeal
     * @param checkgroupIds
     */
    public void setSetmealAndCheckGroupId(Setmeal setmeal, Integer[] checkgroupIds) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                //绑定套餐和检查组的多对多关系
                Map<String,Integer> map = new HashMap<String,Integer>();
                map.put("setmeal_id",setmeal.getId());
                map.put("checkgroup_id",checkgroupId);
                setmealDao.setSetmealAndCheckGroupId(map);
            }
        }
    }
}
